package com.mrduan.otgfilemanager.ui.video;

import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.view.MenuItem;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;

import com.github.mjdev.libaums.fs.UsbFile;
import com.mrduan.otgfilemanager.Config;
import com.mrduan.otgfilemanager.R;
import com.mrduan.otgfilemanager.ui.main.fragment.ThreeScreenFragment;
import com.mrduan.otgfilemanager.ui.video.adapter.ListVideoAdapter;
import com.mrduan.otgfilemanager.ui.video.adapter.VideoConstant;
import com.shike.baselibrary.activity.BaseActivity;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

import cn.jzvd.JZVideoPlayer;

public class ListVideoActivity extends BaseActivity {

    SensorManager sensorManager;
    JZVideoPlayer.JZAutoFullscreenListener sensorEventListener;

    private ListView mVideoList;
    private ListVideoAdapter mAdapter;
    private List<VideoMsgBean> mDatas = new ArrayList<>();

    @Override
    public int getContentView() {
        return R.layout.activity_list_video;
    }

    @Override
    public void initBindWidget() {
        setToolBar(getIntent().getStringExtra("title"),true);
        mVideoList = getView(R.id.list_video);

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        sensorEventListener = new JZVideoPlayer.JZAutoFullscreenListener();
    }

    @Override
    public void initListener() {
        mVideoList.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                JZVideoPlayer.onScrollReleaseAllVideos(view, firstVisibleItem, visibleItemCount, totalItemCount);
            }
        });
    }

    @Override
    public void initData() throws IOException {

        UsbFile[] files = ThreeScreenFragment.usbFile.listFiles();

        List<UsbFile> imgs = new ArrayList<>();
        List<UsbFile> videos = new ArrayList<>();
//        List<String> titles = new ArrayList<>();

        for (UsbFile f : files) {
            if (Config.isVideo(f.getName())) {
                videos.add(f);
            } else {
                imgs.add(f);
            }
        }

        for (int i=0;i<videos.size();i++) {
            mDatas.add(new VideoMsgBean(videos.get(i).getName(),
                    Config.getUrl(videos.get(i)),
                    Config.getUrl(imgs.get(i))));
        }

//        for (int i = 0; i < 7; i++) {
//            mDatas.add(new VideoMsgBean(VideoConstant.videoTitles[i],
//                    VideoConstant.videoUrlList[i],
//                    VideoConstant.videoThumbList[i]));
//        }

        mAdapter = new ListVideoAdapter(this, mDatas);
        mVideoList.setAdapter(mAdapter);
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        Sensor accelerometerSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        sensorManager.registerListener(sensorEventListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(sensorEventListener);
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

}
